class_name CBufferStream

var _data_array: PackedByteArray
var _read_pos: int
var _write_pos: int

func _init(data_array) -> void:
	_data_array = data_array
	_read_pos = 0
	_write_pos = 0

func GetDataSize():
	return _data_array.size()

func SetPosition(read_pos):
	_read_pos = read_pos

func GetPosition():
	return _read_pos

func ReadUint8():
	var u8 = _data_array.decode_u8(_read_pos)
	_read_pos = _read_pos + 1
	return u8

func ReadUint16():
	var u16 = _data_array.decode_u16(_read_pos)
	_read_pos += 2
	return u16

func ReadUint32():
	var u32 = _data_array.decode_u32(_read_pos)
	_read_pos = _read_pos + 4
	return u32

func ReadUint64():
	var u64 = _data_array.decode_u64(_read_pos)
	_read_pos = _read_pos + 8
	return u64

func ReadInt8():
	var i8 = _data_array.decode_s8(_read_pos)
	_read_pos = _read_pos + 1
	return i8

func ReadInt16():
	var i6 = _data_array.decode_s16(_read_pos)
	_read_pos += 2
	return i6

func ReadInt32():
	var u32 = _data_array.decode_s32(_read_pos)
	_read_pos = _read_pos + 4
	return u32

func ReadInt64():
	var u64 = _data_array.decode_s64(_read_pos)
	_read_pos = _read_pos + 8
	return u64

func ReadString(length:int):
	var data = _data_array.slice(_read_pos, _read_pos+length)
	_read_pos = _read_pos + length
	return data.get_string_from_utf8()
